package demo;

import connectionpool.C3P0Executor;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 */
public class TwoClusterTest {

    private static final String url1 = "jdbc:opengauss://192.168.0.243:14100,192.168.0.243:14200/test?" +
        "autoBalance=leastconn&enableQuickAutoBalance=true&maxIdleTimeBeforeTerminal=10&loggerLevel=INFO" +
        "&minReservedConPerCluster=20&minReservedConPerDatanode=30";

    private static final String url2 = "jdbc:opengauss://192.168.0.243:14200,192.168.0.243:14300/test?" +
        "autoBalance=leastconn&enableQuickAutoBalance=true&maxIdleTimeBeforeTerminal=10&loggerLevel=INFO" +
        "&minReservedConPerCluster=20&minReservedConPerDatanode=30";

    private final static String username = "test";

    private final static String password = "huawei123.";

    private static C3P0Executor executor1;

    private static C3P0Executor executor2;

    private static void solve() throws SQLException, InterruptedException {
        int num = 300;
        executor1 = new C3P0Executor(url1, username, password, num, num);
        executor2 = new C3P0Executor(url2, username, password, num, num);

        while (true) {
            List<Connection> list = new ArrayList<>();
            for (int i = 0; i < num; i++) {
                list.add(executor1.getConnection());
                list.add(executor2.getConnection());
            }
            for (Iterator<Connection> iterator = list.iterator(); iterator.hasNext();) {
                Connection connection = iterator.next();
                connection.close();
                iterator.remove();
            }
            Thread.sleep(10 * 1000);
        }
    }

    public static void main(String[] args) throws SQLException, InterruptedException {
        solve();
    }
}
